package com.sdjenkins.exercise.atm;

import com.sdjenkins.exercise.atm.ATMCommandProcessor.Command;
import com.sdjenkins.exercise.atm.ATMCommandProcessor.CommandType;

/**
 * This object translates the input string into a Command object 
 * defined as an Inner Class.
 * 
 * The Command object is used by the ATMCommandProcessor
 * 
 * Added to help clarity of code.  
 * 
 * @author Simon Jenkins
 *
 */
public class ATMCommandInterpreter {
	public Command interpret(String cmdStr) {
		
		CommandType ctyp = null;
		String[] parms = cmdStr.split(" ");
		int num = parms.length;
		if (num == 3 && isInt(parms[0]) && isInt(parms[1]) && isInt(parms[2]) ) {
			ctyp = CommandType.validate;
		}
		else if (num == 1 && isInt(parms[0])) {
			ctyp = CommandType.load;
		}
		else if (num == 1 && parms[0].equals("B")) {
			ctyp = CommandType.balance;
			parms = null;
		}
		else if (num == 2 && parms[0].equals("W")) {
			ctyp = CommandType.withdraw;
			parms = new String[] {parms[1]};
		}
		else if (num == 2 && isInt(parms[0]) && isInt(parms[1]) ) {
			ctyp = CommandType.setBalance;
		}		
		else if (num == 1 && parms[0].equals("") ) {
			ctyp = CommandType.reset;
			parms = null;
		}		
		return new Command(ctyp, parms);		
	}
	private boolean isInt(String test) {
		boolean isint = false;
		try {
			Integer.parseInt(test);
			isint = true;
		}
		catch (Exception e) {
			isint = false;
		}
		return isint;
	}
	
	
}
